package main

import (
	"context"
	"go-locks/no-lock/proto"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"sync"
)

var client proto.StockClient

func main() {
	conn, err := grpc.Dial("127.0.0.1:8088", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		panic(err)
	}
	client = proto.NewStockClient(conn)
	var wg sync.WaitGroup
	wg.Add(20)
	for i := 0; i < 20; i++ {
		go TestSellStock(&wg)
	}
	wg.Wait()
}

func TestSellStock(wg *sync.WaitGroup) {
	defer wg.Done()
	_, err := client.SellStock(context.Background(), &proto.StockRequest{
		StockInfos: []*proto.StockInfo{
			{
				GoodsId: "123456",
				Num:     1,
			},
		},
	})
	if err != nil {
		panic(err)
	}
}
